package cn.bdqn.controller;

import cn.bdqn.converter.PageInfo2SaleVoConverter;
import cn.bdqn.entity.Sale;
import cn.bdqn.entity.User;
import cn.bdqn.enums.JxcEnum;
import cn.bdqn.exception.JxcException;
import cn.bdqn.service.SaleService;
import cn.bdqn.utils.ResultVoUtil;
import cn.bdqn.vo.ResultVo;
import cn.bdqn.vo.SaleVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * 销售记录Controller
 * @author 郑大仙丶
 * @date 2018-12-31 14:54
 */
@RestController // @ResponseBody + @Controller
@RequestMapping("/sale")
public class SaleController {

    @Autowired
    private SaleService saleService;

    /*
    分页查询销售记录并排序
    成功
		{
			code:200,
			message:成功,
			data:
				{
					currentPage : 1,
					pageSize : 5,
					totalCount : 9,
					totalPage : 2,
					list : [
						{
							id:1,
							productName:汤圆,
							price:10.2,
							quantity:10,
							totalprice:102,
							saledate:12338712687,
							realName:詹姆斯
						},
						..
					]
				}
		}
	失败
		{
			code:错误编码,
			message:错误信息,
			data:null
		}

     */
    @RequestMapping(value = "/findAllByPage",method = RequestMethod.GET)
    public ResultVo findAllByPage(
            @RequestParam(value = "currentPage",defaultValue = "1")Integer page,
            @RequestParam(value = "pageSize",defaultValue = "5")Integer size,
            @RequestParam(value = "flag",defaultValue = "0")Integer flag){
        //1. 分页查询
        PageInfo<Sale> pageInfo = saleService.findAllOrderBy(page, size, flag);

        //2. 封装数据
        SaleVo data = PageInfo2SaleVoConverter.convert(pageInfo);

        //3. 响应
        return ResultVoUtil.success(data);
    }


    /*
    ``
    路径
    POST	/InSystem/sale/addSale
    ``
    参数
    productId:1
    price:10.2
    quantity:10
    ``
    响应数据
    成功
        {
            code : 200,
            message : 成功,
            data : null
        }
     */
    @RequestMapping(value = "/addSale",method = RequestMethod.POST)
    public ResultVo addSale(HttpSession session, @Valid Sale sale,
                            BindingResult result){
        // 1. 参数校验
        if(result.hasErrors()){
            // 记录日志.
            throw new JxcException(
                    JxcEnum.PARAM_ERROR.getCode(),
                    result.getFieldError().getDefaultMessage());
        }
        //2. 封装数据
        User user = (User) session.getAttribute("users");
        if(user == null){
            // 用户未登录.
            throw new JxcException(JxcEnum.USER_NOT_LOGIN);
        }
        sale.setUserId(user.getId());
        //3. 调用service保存
        saleService.save(sale);
        //4. 响应数据
        return ResultVoUtil.success();
    }









}
